<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\MConfig;
use App\Models\MLog;
use App\Utils\ComUtil;
use Illuminate\Http\Request;

/**
 * 系统设置控制器
 */
class SettingController extends Controller
{
    /**
     * 默认系统欢迎页
     */
    public function welcomePage()
    {
        return view("admin.welcome");
    }

    /**
     * 网站信息配置界面
     */
    public function settingPage()
    {
        return view("admin.web_setting");
    }

    /**
     * 网站配置信息保存
     */
    public function settingSave(Request $request)
    {

        if (!$this->validateReq($request, MConfig::webRulesWithMsg(0), MConfig::webRulesWithMsg(1))) {

            $this->data = $this->makeJsCode(1, $this->errors->first());

            return response()->json($this->data);
        }
        if (MConfig::saveKV("config_web_setting", $request->all())) {
            $this->data = $this->makeJsCode(0, "保存成功！");
            ComUtil::initConfig2Cache("config_web_setting", true);    //需手工刷新下配置信息
        } else {
            $this->data = $this->makeJsCode(1, "保存失败！");
        }
        return response()->json($this->data);
    }

    /**
     * 系统操作日志
     */
    public function logsPage(Request $request, $index = 1)
    {
        //初始化分页
        $this->fyInit(route("admin.logs"), 20, $request->getQueryString());
        $query = (new MLog())->query();

        //按条件筛选
        if ($request->has(['search_type','search_str'])) {
            if ($request->search_type == 1) { //管理员搜索(精确搜索)
                $query = $query->where('username', $request->search_str);
            } elseif ($request->search_type == 2) {  //ip地址搜索(精确搜索)
                $query = $query->where('ip', $request->search_str);
            }
        }

        $this->fyCalc($query->count(), $index);
        $this->data['logs'] = $query->orderBy('time', 'desc')
            ->skip($this->fy_pgOffset)->take($this->fy_pgSize)
            ->get();

        //分页视图初始化
        $this->fyViewInit($this->data);
        return view('admin.logs')->with($this->data);
    }

    /**
     * 日志删除处理
     */
    public function logsDel($id)
    {
        Mlog::destroy($id);
        $this->data = $this->makeJsCode(0, "删除成功！");
        return back()->with($this->data);
    }

    /**
     * 网站基本信息配置界面
     */
    public function basicSetPage ()
    {
        return view ('admin.basic_setting');
    }

    /**
     * 网站基本信息配置保存
     */
    public function basicSave(Request $request)
    {

        if (!$this->validateReq($request, MConfig::basicRulesWithMsg(0), MConfig::basicRulesWithMsg(1))) {

            $this->data = $this->makeJsCode(1, $this->errors->first());

            return response()->json($this->data);
        }
        if (MConfig::saveKV("config_basic_setting", $request->all())) {
            $this->data = $this->makeJsCode(0, "保存成功！");
            ComUtil::initConfig2Cache("config_basic_setting", true);    //需手工刷新下配置信息
        } else {
            $this->data = $this->makeJsCode(1, "保存失败！");
        }
        return response()->json($this->data);
    }
}